package com.clkj.zhonghui.controller.topic;

import com.clkj.zhonghui.pojo.common.Result;
import com.clkj.zhonghui.pojo.common.StatusCode;
import com.clkj.zhonghui.pojo.topic.Topic;
import com.clkj.zhonghui.pojo.topic.TopicComment;
import com.clkj.zhonghui.pojo.topic.TopicPrise;
import com.clkj.zhonghui.service.topic.TopicService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author：周佳润
 * @Date：2024/7/16 上午9:24
 */
@CrossOrigin
@RestController
@RequestMapping("/topic")
@Api(value = "/topic", tags = {"文章管理"})
public class TopicController {

    @Autowired
    private TopicService topicService;

    @PostMapping("/getTopicPage")
    @ApiOperation(value = "获取文章视频列表")
    public Result getTopicPage(String topicTitle, Integer topicFlag,Integer page, Integer size) {
        try {
            PageInfo topicList = topicService.getTopicPage(topicTitle,topicFlag, page, size);
            return new Result(true, StatusCode.OK, "查询成功", topicList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询成功", "error");
        }
    }

    @PostMapping("/addTopic")
    @ApiOperation(value = "添加/修改 文章视频")
    public Result  addTopic(Topic topic,String flag,HttpServletRequest request){
        try {
            topicService.addTopic(topic,request,flag);
            return new Result(true,StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }

    }

    @ApiOperation(value = "启用禁用文章和视频")
    @PostMapping("/updateTopic")
    public Result updateTopic(String ids,Integer topicStatus){
        try {
            topicService.updateTopic(ids,topicStatus);
            return new Result(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }


    @ApiOperation(value = "删除文章视频")
    @PostMapping("/delTopic")
    public Result delTopic(String ids){
        try {
            topicService.delTopic(ids);
            return new Result(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/getTopicComment")
    @ApiOperation(value = "获取文章视频评论列表")
    public Result getTopicComment(@RequestParam Integer commentFlag,
                                  Integer topicId,
                                      @RequestParam Integer pageNumber,
                                      @RequestParam Integer pageSize) {
        try {
            PageInfo comment = topicService.getTopicComment(commentFlag, topicId,pageNumber, pageSize);
            return new Result(true, StatusCode.OK, "查询成功", comment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }

    @ApiOperation(value = "删除评论")
    @PostMapping("/delComment")
    public Result delComment(String ids){
        try {
            topicService.delComment(ids);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @PostMapping("/addComment")
    @ApiOperation(value = "添加/修改评论")
    public Result addComment(TopicComment topicComment, String flag){
        try {

            topicService.addComment(topicComment,flag);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "评论导表")
    @PostMapping("/importComment")
    public Result importComment(HttpServletRequest request){
        try {
            topicService.importComment(request);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @ApiOperation(value = "评论置顶")
    @PostMapping("/zhidingComment")
    public Result zhidingComment(String ids,Integer topping){
        try {
            topicService.zhidingComment(ids,topping);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @PostMapping("/getTopicById")
    @ApiOperation(value = "获取单个文章或视频信息")
    public Result getTopicById(@ApiParam(name = "topicId", value = "文章或视频id") @RequestParam("topicId") Integer topicId) {
        try {
            Topic topic  = topicService.getTopicById(topicId);
            return new Result(true, StatusCode.OK, "查询成功",topic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "添加失败");
    }

    @PostMapping("/addTopicPrise")
    @ApiOperation(value = "点赞/取消点赞")
    public Result addTopicPrise(TopicPrise topicPrise){
        try {

            topicService.addTopicPrise(topicPrise);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @PostMapping("/listTopicPrise")
    @ApiOperation(value = "查询点赞数")
    public Result listTopicPrise(Integer topicId){
        try {

            topicService.listTopicPrise(topicId);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @PostMapping("/getAppTopicById")
    @ApiOperation(value = "APP文章视频详情")
    public Result getAppTopicById(Integer topicId,Integer userId) {
        try {
            Map comment = topicService.getAppTopic(topicId,userId);
            return new Result(true, StatusCode.OK, "查询成功", comment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }


}
